package com.rongxun.main.ui.a;


import android.view.MenuItem;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.rongxun.api.utils.FlyLog;
import com.rongxun.common.aroute.RouteUtil;
import com.rongxun.common.aroute.provider.IFindProvider;
import com.rongxun.common.aroute.provider.IMeProvider;
import com.rongxun.common.aroute.provider.INewsProvider;
import com.rongxun.common.base.mvvm.BaseRxActivity;
import com.rongxun.main.R;
import com.rongxun.main.entity.MainChannel;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;


/**
 * 创建者     forza
 * 创建时间   2019/11/29 18:09
 * 描述
 */


@Route(path = RouteUtil.MAIN_A)
public class MainActivity extends BaseRxActivity {
    @Autowired(name = "/news/main")
    INewsProvider mNewsProvider;

    @Autowired(name = "/find/main")
    IFindProvider mFindProvider;

    @Autowired(name = "/me/main")
    IMeProvider mMeProvider;

    private Fragment mNewsFragment;
    private Fragment mFindFragment;
    private Fragment mMeFragment;

    private Fragment mCurrFragment;

    @Override
    protected void initContentView() {
        setContentView(R.layout.activity_main);
    }


    @Override
    public void init() {

        FlyLog.d("我是A面");


        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int i = menuItem.getItemId();
                if (i == R.id.navigation_trip) {
                    switchContent(mCurrFragment, mNewsFragment, MainChannel.NEWS.name);
                    mCurrFragment = mNewsFragment;

                    return true;
                } else if (i == R.id.navigation_discover) {
                    switchContent(mCurrFragment, mFindFragment, MainChannel.FIND.name);
                    mCurrFragment = mFindFragment;

                    return true;
                } else if (i == R.id.navigation_me) {
                    switchContent(mCurrFragment, mMeFragment, MainChannel.ME.name);
                    mCurrFragment = mMeFragment;

                    return true;
                }
                return false;
            }
        });
        if (mNewsProvider != null) {
            mNewsFragment = mNewsProvider.getMainNewsFragment();
        }
        if (mFindProvider != null) {
            mFindFragment = mFindProvider.getMainFindFragment();
        }
        if (mMeProvider != null) {
            mMeFragment = mMeProvider.getMainMeFragment();
        }
        mCurrFragment = mNewsFragment;
        if (mNewsFragment != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, mNewsFragment, MainChannel.NEWS.name).commit();
        }
    }

    public void switchContent(Fragment from, Fragment to, String tag) {
        if (from == null || to == null) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!to.isAdded()) {
            transaction.hide(from).add(R.id.frame_content, to, tag).commit();
        } else {
            transaction.hide(from).show(to).commit();
        }
    }

    @Override
    public void showInitLoadView(boolean show) {

    }

    @Override
    public void showNoDataView(boolean show) {

    }

    @Override
    public void showTransLoadingView(boolean show) {

    }

    @Override
    public void showNetWorkErrView(boolean show) {

    }
}
